<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item label="用户ID" prop="userId">
          <el-input
            v-model="formData.userId"
            placeholder="请输入用户ID"
          />
        </el-form-item>
        <el-form-item label="服务名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入服务名称，如“余额”、“优惠券”等"
          />
        </el-form-item>
        <el-form-item label="服务内容" prop="content">
          <Editor v-model="formData.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="服务描述" prop="description">
          <Editor v-model="formData.description" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">
          确 定
        </el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ServiceVipservicesApi from "@/api/system/servicevipservices";
import Editor from "@/components/Editor";

export default {
  name: "ServiceVipservicesForm",
  components: {
    Editor,
  },
  data() {
    return {
      dialogTitle: "",
      dialogVisible: false,
      formLoading: false,
      formData: {
        id: undefined,
        name: undefined,
        description: undefined,
        content: undefined,
        userId: undefined,
      },
      formRules: {
        userId: [
          {
            required: true,
            message: "用户ID不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "服务名称，如“余额”、“优惠券”等不能为空",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "服务描述不能为空",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "服务内容不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      if (id) {
        this.formLoading = true;
        try {
          const res = await ServiceVipservicesApi.getServiceVipservices(id);
          this.formData = res.data;
          this.dialogTitle = "修改VIP服务";
        } finally {
          this.formLoading = false;
        }
      }
      this.dialogTitle = "新增VIP服务";
    },
    async submitForm() {
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        if (data.id) {
          await ServiceVipservicesApi.updateServiceVipservices(data);
          this.$modal.msgSuccess("修改成功");
        } else {
          await ServiceVipservicesApi.createServiceVipservices(data);
          this.$modal.msgSuccess("新增成功");
        }
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        description: undefined,
        content: undefined,
        userId: undefined,
      };
      this.$nextTick(() => {
        this.$refs.formRef?.resetFields();
      });
    },
  },
};
</script>